package com.zzl.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.springboot.entity.Role;
import com.zzl.springboot.entity.User;
import com.zzl.springboot.entity.UserInfo;
import com.zzl.springboot.mapper.RoleMapper;
import com.zzl.springboot.mapper.UserInfoMapper;
import com.zzl.springboot.mapper.UserMapper;
import com.zzl.springboot.mapper.VolMapper;
import com.zzl.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: AAAzzl
 * @description:
 * @author: zzl
 * @create: 2022-11-19 11:02
 * @version:1.0
 **/

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper infoMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private VolMapper volMapper;

    @Override
    public Integer getVol1() {
        return userMapper.getVol1();
    }

    @Override
    public Integer getVol2() {
        return userMapper.getVol2();
    }

    @Override
    public Integer getVol3() {
        return userMapper.getVol3();
    }

    @Override
    public Integer getVolNum() {
        return userMapper.getVolNum();
    }

    @Override
    public PageInfo<User> getUserList(int pageNum, int pageSize) {
        //开始分页     后面的第一条查询语句才会被分页处理
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectAll();
        return new PageInfo<>(users);
    }

    @Override
    public UserInfo getInfoByUserId(Integer id) {
        return infoMapper.selectByUserId(id);
    }

    @Override
    public List<Role> getRoleList() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo<User> getListByRoleId(int pageNum, int pageSize, int roleId) {
        //开始分页     后面的第一条查询语句才会被分页处理
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectByRoleId(roleId);
        return new PageInfo<>(users);
    }

    @Override
    public PageInfo<User> getUserListBySearch(int pageNum, int pageSize, int roleId, String searchInfo) {
        //开始分页     后面的第一条查询语句才会被分页处理
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectBySearch(roleId,searchInfo);
        return new PageInfo<>(users);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Integer delById(Integer id) {
        //fixme: 此处删除用户应该连同用户信息一起删去，但个人做了保留，因为总服务数不能因为用户的删除而减少
//        infoMapper.delByUserId(id);
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer delByChecked(String[] ids) {
        return userMapper.delByChecked(ids);
    }

    @Override
    public Integer addUser(User user) {
        UserInfo info = user.getUserInfo();
        // 插入账号信息
        int insert = userMapper.addUser(user);
        // 将返回的id赋值给info
        info.setUserId(user.getId());
        // 将用户信息插入数据库
        int insert1 = infoMapper.insert(info);
        // 返回响应行数
        return insert;
    }

    @Override
    public User getByUsername(String username) {
        return userMapper.selectByName(username);
    }

    @Override
    public Integer userRegister(User user) {
        UserInfo info = user.getUserInfo();
        // 插入账号信息
        user.setCreateTime(new Date());
        user.setRoleId(1);
        user.setState((byte) 0);
        int insert = userMapper.addUser(user);
        // 将返回的id赋值给info
        info.setUserId(user.getId());
        // 给用户添加初始名字
        info.setName("用户"+((int)(Math.random()*90000+10000)));
        info.setSex((byte) 0);
        info.setVolNum(0);
        // 将用户信息插入数据库
        int insert1 = infoMapper.insert(info);
        // 返回响应行数
        return insert;
    }

    @Override
    public Integer updateInfo(UserInfo userInfo) {
        return infoMapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public Integer getVolByUserId(Integer id) {
        return volMapper.selectByUserIdCount(id);
    }

    @Override
    public Integer getVolUserByUserId(Integer id) {
        return volMapper.selectVolUserCountByUserId(id);
    }


}
